package org.light.framework.bean;


import java.util.ArrayList;

/**
 * 查询返回的结果列
 * @author de.cel
 *
 */
public class ResultColumn {

	private ArrayList<String> includes = null;
	private ArrayList<String> excludes = null;

	public ResultColumn() {
		includes = new ArrayList<String>();
		excludes = new ArrayList<String>();
	}

	/**
	 * 添加需要查询的属性名
	 *
	 */
	public void add(String propertyName) {
		if (!includes.contains(propertyName))
			includes.add(propertyName);
	}

	/**
	 * 删除不需要查询的属性名
	 *
	 */
	public void delete(String propertyName) {
		if (!excludes.contains(propertyName))
			excludes.add(propertyName);
	}

	/**
	 * 结果中是否包含指定的属性名
	 * 
	 * @param propertyName 属性名或列名
	 * @return
	 */
	public boolean include(String propertyName) {

		if (!includes.isEmpty() && propertyName != null) {
			for (String t : includes) {
				if (t.startsWith(propertyName)) {
					return true;
				}
			}
			return false;
		}
		if (propertyName != null) {
			for (String t : excludes) {
				if (propertyName.startsWith(t)) {
					return false;
				}
			}
		}
		return true;
	}

	/// <summary>
	/// 是否为空（未设置查询结果项，表示查询全部）
	/// </summary>
	public boolean isEmpty() {
		return (includes == null || includes.isEmpty()) && (excludes == null || excludes.isEmpty());
	}

	public String toString() {
		StringBuilder builder = new StringBuilder();
		if (!includes.isEmpty()) {
			for (String c : includes) {
				builder.append(c + ",");
			}
			builder.delete(builder.length() - 1, 1);
		} else {
			builder.append(" * ");
		}
		return builder.toString();
	}
}
